package vn.hocweb.dao.dao_implement;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vn.hocweb.dao.dao_interface.ICongCuDAO;
import vn.hocweb.entity.CongCu;

public class CongCuDAO extends GenericDAO implements ICongCuDAO{

	@Override
	public CongCu findByID(Long id) {
		Connection connection = getConnection();
		CongCu result = null;
		String sql = "select * from congcu where macongcu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, id);
			ResultSet effected = pre.executeQuery();
			if (effected.next()){
				long maCongCu = effected.getLong("macongcu");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String hinhAnh = effected.getString("hinhanh");
				String duongDan = effected.getString("duongdan");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int luotTaiVe = effected.getInt("luottaive");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				
				result = new CongCu(maCongCu, nguoiCapNhat, ten, moTa,
						hinhAnh, duongDan, ngayCapNhat, luotTaiVe, 
						thuTu, noiBat, hienThi);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<CongCu> findAll(int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<CongCu> result = new ArrayList<CongCu>();
		String sql = "select * from congcu limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, firstItem);
			pre.setLong(2, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maCongCu = effected.getLong("macongcu");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String hinhAnh = effected.getString("hinhanh");
				String duongDan = effected.getString("duongdan");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int luotTaiVe = effected.getInt("luottaive");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				
				CongCu row = new CongCu(maCongCu, nguoiCapNhat, ten, moTa,
						hinhAnh, duongDan, ngayCapNhat, luotTaiVe, 
						thuTu, noiBat, hienThi);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public int size() {
		Connection connection = getConnection();
		int result = 0;
		String sql = "select count(*) from congcu";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			ResultSet effected = pre.executeQuery();
			if(effected.next()){
				result = effected.getInt(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean save(CongCu entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "insert into congcu (ten, mota, hinhanh, duongdan, ngaycapnhat, " +
				"luottaive, thutu, noibat, hienthi, nguoicapnhat) " +
				"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getTen());
			pre.setString(2, entity.getMoTa());
			pre.setString(3, entity.getHinhAnh());
			pre.setString(4, entity.getDuongDan());
			pre.setDate(5, entity.getNgayCapNhat());
			pre.setInt(6, entity.getLuotTaiVe());
			pre.setInt(7, entity.getThuTu());
			pre.setBoolean(8, entity.isNoiBat());
			pre.setBoolean(9, entity.isHienThi());
			pre.setString(10, entity.getNguoiCapNhat().getTenDangNhap());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean delete(CongCu entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "delete from congcu where macongcu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, entity.getMaCongCu());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean remove(CongCu entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update congcu set hienthi = ? where macongcu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, false);
			pre.setLong(2, entity.getMaCongCu());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean restore(CongCu entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update congcu set hienthi = ? where macongcu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, true);
			pre.setLong(2, entity.getMaCongCu());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

}
